package com.bes.aiProject.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bes.aiProject.entry.SxAAbnormalResult;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SxAAbnormalResultMapper extends BaseMapper<SxAAbnormalResult> {

    @Select("select  \n" +
            "round (sum( case when t1.abnormal_type=t2.abnormal then 1 else 0 end )/count(*)*100) as score,\n" +
            "${groupCondition} ,\n" +
            "metric_code ,\n" +
            "metric_name ,\n" +
            "abnormal_time \n" +
            "from sx_a_abnormal_result t1 \n" +
            "left join (select distinct metric_code as metric,abnormal_type as abnormal from sx_m_metric_conf \n" +
            "group by  ${groupCondition} ,metric_code ,metric_name ,abnormal_time")
    List<SxAAbnormalResult> getAllScoreByGroup(@Param("groupCondition") String groupCondition);


    @Select("select distinct ${groupCondition} from sx_a_abnormal_result")
    List<SxAAbnormalResult> getAbnormalConditionGroup(@Param("groupCondition") String groupCondition);

    @Select("<script>\t\n" +
            "select  \n" +
            "t1.* ,\n" +
            "round (sum( case when t1.abnormal_type != t2.abnormal then 1 else 0 end )/ count(*)* 100) as score\n" +
            "from \n" +
            "sx_a_abnormal_result t1 \n" +
            "left join \n" +
            "(select distinct metric_code as metric,abnormal_type as abnormal from sx_m_metric_conf) t2\n" +
            "on t1.metric_code =t2.metric \n" +
            "where 1=1 \n" +
            "<if test=“areaCode != null and areaCode != ‘’”>\n" +
            "and area_code = #{areaCode}\n" +
            "</if> \n" +
            "<if test=“customerType != null and customerType != ‘’”>\n" +
            "and customer_type = #{customerType}\n" +
            "</if> \n" +
            "<if test=“enterpriseCode != null and enterpriseCode != ‘’”>\n" +
            "and enterprise_code = #{enterpriseCode}\n" +
            "</if> \n" +
            "<if test=“businessCode != null and businessCode != ‘’”>\n" +
            "and business_code = #{businessCode}\n" +
            "</if> \n" +
            "<if test=“taskCode != null and taskCode != ‘’”>\n" +
            "and task_code = #{taskCode}\n" +
            "</if> \n" +
            "group by \n" +
            "<if test=“areaCode != null and areaCode != ‘’”>\n" +
            "area_code ,\n" +
            "</if> \n" +
            "<if test=“customerType != null and customerType != ‘’”>\n" +
            "customer_type ,\n" +
            "</if> \n" +
            "<if test=“enterpriseCode != null and enterpriseCode != ‘’”>\n" +
            "enterprise_code ,\n" +
            "</if> \n" +
            "<if test=“businessCode != null and businessCode != ‘’”>\n" +
            "business_code ,\n" +
            "</if> \n" +
            "<if test=“taskCode != null and taskCode != ‘’”>\n" +
            "task_code ,\n" +
            "</if> \n" +
            "metric_code ,abnormal_time\n" +

            "</script>")
    List<SxAAbnormalResult> getScoreByGroup(@Param("areaCode") String areaCode, @Param("customerType") String customerType, @Param("enterpriseCode") String enterpriseCode, @Param("businessCode") String businessCode, @Param("taskCode") String taskCode);
}
